package com.shaozi.nc.execpool;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import com.shaozi.lib.throwable.SzException;

public class IMPushPool {
	private final Logger logger = Logger.getLogger(IMPushPool.class);
	private ImPushThread thread = null;
	private String brokerUrl;
	private String brokerUserName;
	private String brokerPwd;
	private String brokerClientId;

	public IMPushPool() {
		
	}

	public void start() {
		thread = new ImPushThread();
		thread.setDaemon(true);
		thread.setName(IMPushPool.class.getSimpleName());
		thread.start();
	}

	public void stop() {
		if ((thread == null) || (!thread.isAlive())) {
			thread = null;
			return;
		}
		thread.interrupt();
	}

	public String getBrokerUrl() {
		return brokerUrl;
	}

	public void setBrokerUrl(String brokerUrl) {
		this.brokerUrl = brokerUrl;
	}

	private class ImPushThread extends Thread {

		@Override
		public void run() {
			super.run();
			ActiveMQConnectionFactory connectionFactory = null;
			if (StringUtils.isEmpty(brokerUserName) || (StringUtils.isEmpty(brokerPwd))) {
				connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
			} else {
				connectionFactory = new ActiveMQConnectionFactory(brokerUserName, brokerPwd, brokerUrl);
			}

			try {
				Connection connection = connectionFactory.createConnection();
				// 持久的消息
				connection.setClientID(brokerClientId);
				connection.start();
				Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
				Topic topic = session.createTopic(getTopicName());
				MessageConsumer consumer = session.createDurableSubscriber(topic, getSubsciberName());
				consumer.setMessageListener(new ConsumerMessageListener());
			} catch (JMSException e) {
				logger.error(SzException.getStackMessage(e));
			}
		}
	}

	private class ConsumerMessageListener implements MessageListener {

		@Override
		public void onMessage(Message msg) {
			MapMessage mm = (MapMessage) msg;
			try {
				Enumeration en = mm.getMapNames();
				while (en.hasMoreElements()) {
					System.out.println(en.nextElement());
				}
			} catch (JMSException e) {
				logger.error(SzException.getStackMessage(e));
			}
		}

	}

	private String getTopicName() {
		return "im_topic_" + "100000000";
	}

	private String getSubsciberName() {
		return "im_subsciber_" + "100000000";
	}

	public String getBrokerUserName() {
		return brokerUserName;
	}

	public void setBrokerUserName(String brokerUserName) {
		this.brokerUserName = brokerUserName;
	}

	public String getBrokerPwd() {
		return brokerPwd;
	}

	public void setBrokerPwd(String brokerPwd) {
		this.brokerPwd = brokerPwd;
	}

	public String getBrokerClientId() {
		return brokerClientId;
	}

	public void setBrokerClientId(String brokerClientId) {
		this.brokerClientId = brokerClientId;
	}
}
